package com.itlong.cloud.controller.whitelist;

import com.itlong.cloud.POJO.DTO.operate.OperateDeviceWhiteListAuthRulesDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateDeviceWhiteListAuthRulesVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.equip.operate.IOperateDeviceWhiteListAuthRulesServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.encrypt.DesEncryptUtil;
import com.itlong.cloud.utils.encrypt.Md5EncryptUtil;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.NumberHandlerUtil;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;

/**
 * <desc>
 *      运营-设备白名单管理控制器
 * </desc>
 *
 * @createDate 2019/12/11
 **/
@RestController
@RequestMapping("/device/whiteList/authRules")
public class OperateWhiteListAuthRulesController {

    @Autowired
    IOperateDeviceWhiteListAuthRulesServiceClient iOperateDeviceWhiteListAuthRulesServiceClient;

    /**
     * <desc>
     *      保存设备白名单授权文件规则信息
     * </desc>
     *
     * @param dto 设备白名单授权文件规则DTO
     * @return
     * @Author Juguang.S
     * @createDate  2020/08/11
     */
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_DEVICE_WHITE_LIST_AUTH_RULES_SAVE_LOG_DESC)
    @RequestMapping( value = "saveWhiteListAuthRule", method = RequestMethod.POST)
    public Object save(OperateDeviceWhiteListAuthRulesDTO dto){
        try {
            HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            String myUserName = request.getAttribute("myUserName") == null ? null : request.getAttribute("myUserName").toString();
            dto = HttpProtocolUtil.parseRequestParamToDTO(OperateDeviceWhiteListAuthRulesDTO.class,request);
            dto.setLoginUserId(myUserName);
            if (StringUtils.isBlank(dto.getPassword()) || StringUtils.isBlank(dto.getStartTime())
                ||StringUtils.isBlank(dto.getEndTime())|| dto.getAmount() == null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            String psd = Base64.encode(DesEncryptUtil.Encrypt(URLEncoder.encode(new String(dto.getPassword().getBytes()), "utf-8").getBytes(), PlatformConstants.OPERATE_USER_PASSWORD_KEY));
            dto.setPassword(psd);
            //校验同一时间内的密码不能重复问题
            if(iOperateDeviceWhiteListAuthRulesServiceClient.checkPassword(dto)>0){
                return new MessageVO(OperateErrorCodeEnum.AUTH_FILE_PASSWORD_IS_NOT_SAME_ERR.getErrorCode());
            }
            iOperateDeviceWhiteListAuthRulesServiceClient.save(dto);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营-设备白名单授权文件规则管理-save】保存设备白名单授权文件规则信息失败",e);
        }
    }

    /**
     * <desc>
     *      分页获取设备白名单授权文件规则信息
     * </desc>
     *
     * @param dto 设备白名单DTO
     * @return
     * @Author Juguang.S
     * @createDate  2020/08/11
     */
    @RequestMapping(value = "getWhiteListAuthRuleByPage", method = RequestMethod.POST)
    public Object getWhiteListAuthRulesByPage(OperateDeviceWhiteListAuthRulesDTO dto){
        try {
            HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            String myUserName = request.getAttribute("myUserName") == null ? null : request.getAttribute("myUserName").toString();
            dto = HttpProtocolUtil.parseRequestParamToDTO(OperateDeviceWhiteListAuthRulesDTO.class,request);
            dto.setLoginUserId(myUserName);
            Page<OperateDeviceWhiteListAuthRulesVO> page = iOperateDeviceWhiteListAuthRulesServiceClient.getWhiteListAuthRulesByPage(dto);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        }catch(Exception e){
            throw new DataAccessException("【运营-设备白名单授权文件规则-getWhiteListByPage】分页获取设备白名单授权文件规则信息失败",e);
        }
    }

    /**
     * <desc>
     *      禁用启用设备白名单授权文件规则信息
     * </desc>
     *
     * @param dto 设备白名单授权文件规则DTO
     * @return
     * @Author Juguang.S
     * @createDate  2020/08/11
     */
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_DEVICE_WHITE_LIST_AUTH_RULES_UPDATE_LOG_DESC)
    @RequestMapping( value = "updateWhiteListAuthRuleStatus", method = RequestMethod.POST)
    public Object forbidden(OperateDeviceWhiteListAuthRulesDTO dto){
        try {
            HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            String myUserName = request.getAttribute("myUserName") == null ? null : request.getAttribute("myUserName").toString();
            dto = HttpProtocolUtil.parseRequestParamToDTO(OperateDeviceWhiteListAuthRulesDTO.class,request);
            dto.setLoginUserId(myUserName);
            if (StringUtils.isBlank(dto.getRuleId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (!NumberHandlerUtil.isInRange(dto.getStatus(),1,2)){
                return new MessageVO(BaseErrorCodeEnum.ERROR.getErrorCode());
            }
            iOperateDeviceWhiteListAuthRulesServiceClient.forbidden(dto);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());

        }catch (Exception e){
            return new DataAccessException("【运营-设备白名单授权文件规则管理forbidden】禁用启用设备白名单规则信息失败",e);
        }
    }
}
